package com.ptit.piclient;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

import com.ptit.piclient.model.Artist;
import com.ptit.piclient.tools.AllArtistAdapter;
import com.ptit.piclient.tools.PiConnector;

public class AllArtistActivity extends Activity implements TextWatcher,
		OnItemClickListener {
	ListView lv;
	EditText txtSearchArtist;
	ArrayList<Artist> list = new ArrayList<Artist>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_artist_list);
		lv = (ListView) findViewById(R.id.allArtistListView);
		lv.setOnItemClickListener(this);
		txtSearchArtist = (EditText) findViewById(R.id.txtSearchArtist);
		txtSearchArtist.addTextChangedListener(this);
		new UpdateView(this).execute("");
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
//		Artist artist = list.get(position);
//		new PlayArtistAsync(this).execute(artist);
//		Intent intent = new Intent(this, NowPlayingActivity.class);
//		startActivity(intent);
		//Oct 8
		Intent intent = new Intent(this,SA_Artist_Details.class);
		intent.putExtra("artistid", list.get(position).getArtistID());
		intent.putExtra("artistname",list.get(position).getArtistName());
		startActivity(intent);
		//End Oct 8
	}

//	private class PlayArtistAsync extends AsyncTask<Artist, Void, Void> {
//		Activity act = null;
//
//		// Album album;
//		public PlayArtistAsync(Activity ac) {
//			act = ac;
//			// album = al;
//		}
//
//		@Override
//		protected Void doInBackground(Artist... params) {
//			PiConnector.getInstance(act).playArtist(params[0]);			 
//			return null;
//		}
//		//khuongdv start sep28
//		@Override
//		protected void onPostExecute(Void result) {
//			Intent intent = new Intent(act, NowPlayingActivity.class);		
//			startActivity(intent);
//		}
//		//khuongdv end sep28
//	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void afterTextChanged(Editable arg0) {

	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		String key = txtSearchArtist.getText().toString();
		new UpdateView(this).execute(key);
	}

	/*
	 * New thread to update listview
	 */
	private class UpdateView extends AsyncTask<String, Void, AllArtistAdapter> {
		Activity act;
		ProgressDialog dialog;

		public UpdateView(Activity a) {
			act = a;
			dialog = new ProgressDialog(AllArtistActivity.this);
			dialog.setTitle("");
			dialog.setMessage("Please wait...");
			dialog.setIndeterminate(true);
			dialog.show();
		}

		@Override
		protected void onProgressUpdate(Void... params) {
			

		}

		@Override
		protected AllArtistAdapter doInBackground(String... params) {
			// Oct 11
			if(params[0].equals("") == false ) dialog.dismiss();
			// Oct11 end
			list = PiConnector.getInstance(act).getArtistsByKeyword(params[0]);// getAllSongsFromPI();
			AllArtistAdapter ad = new AllArtistAdapter(act, list);
			return ad;
		}

		@Override
		protected void onPreExecute() {

		}

		protected void onPostExecute(AllArtistAdapter result) {
			dialog.dismiss();
			if (result != null)
				lv.setAdapter(result);
		}

	}
}
